#include <bits/stdc++.h>

#define int long long
#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 2e3 + 10;

int n, K, a[N], b[N], c[N], d[N], tot, pot[N], tdis[N];

namespace F { // max_flow_with_cost
    const int inf = 0x3f3f3f3f;
	const int N = 1e4 + 10;
	const int M = 2e6 + 10;
    int h[N], now[N], cnt = 1, S, T;
    bool vis[N];
    ll maxflow, cost, dis[N];
    struct edge { int v, w, c, nxt; } e[M << 1];
    inline void tlink(int x, int y, int w, int c) { e[++cnt] = (edge) { y, w, c, h[x] }; h[x] = cnt; }
    inline void link(int x, int y, int w, int c) { tlink(x, y, w, c); tlink(y, x, 0, -c); }
    inline void setst(int s, int t) { S = s; T = t; }
    bool bfs() {
		queue < int > q; q.push(S); memset(dis, 0x3f, sizeof dis); memset(vis, 0, sizeof vis); dis[S] = 0; now[S] = h[S]; vis[S] = 1;
		while(!q.empty()) {
			int x = q.front(); q.pop();
			for(int i = h[x], y; i; i = e[i].nxt)
				if(e[i].w && dis[y = e[i].v] > dis[x] + e[i].c) {
					dis[y] = dis[x] + e[i].c; now[y] = h[y];
					if(!vis[y]) vis[y] = 1, q.push(y);
				}
			vis[x] = 0;
		} return dis[T] < inf;
    }
    int dinic(int x,int flow,ll c) {
		if(x == T) return maxflow += flow, cost += c * flow, flow;
		int res = flow; vis[x] = 1;
		for(int i = now[x], y; i && res; i = e[i].nxt) {
			now[x] = i;
			if(e[i].w && dis[y = e[i].v] == dis[x] + e[i].c && !vis[y]) {
				int k = dinic(y, min(res,e[i].w), c + e[i].c); 
				e[i].w -= k; e[i ^ 1].w += k; res -= k;
			}
		} vis[x] = 0;
		return flow - res;
    }
    void runit() { while(bfs()) dinic(S, inf, 0); }
}

int pos(int x) { return lower_bound(pot + 1, pot + tot + 1, x) - pot; }
vec G[N];

ll sq(ll x) { return x * x; }
int dis(int i) { return sqrt(sq(d[i] - b[i]) + sq(c[i] - a[i])); }

signed main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, K = in; rep(i, 1, n) a[i] = in, b[i] = in, c[i] = in, d[i] = in, tdis[i] = dis(i),
						(a[i] > c[i] && (swap(a[i], c[i]), swap(b[i], d[i]), 1)), a[i] <<= 1, c[i] <<= 1,
						(a[i] == c[i] ? c[i]++ : a[i]++), pot[++tot] = a[i], pot[++tot] = c[i];
	sort(pot + 1, pot + tot + 1); tot = unique(pot + 1, pot + tot + 1) - pot - 1;
	rep(i, 1, n) a[i] = pos(a[i]), c[i] = pos(c[i]), G[a[i]].eb(i);
	F :: setst(0, tot + 1);
	rep(i, 0, tot) {
		F :: link(i, i + 1, K, 0);
		for(auto v : G[i]) F :: link(i, c[v], 1, -tdis[v]);
	} F :: runit(); printf("%lld\n", -F :: cost);
	return 0;
}
